<?php 
    include_once("config/config.php");
    if ($_POST) {
        // 获取输入的验证码
        $imgcode = $_POST['imgcode'];
        $username = $_POST['username'];
        $password = $_POST['password'];

        $sql = "select * from admin where username = '$username'";
        $admin = findOne($sql);

        if (!$admin) {
            ShowMsg("该用户不存在");
            exit;
        }
        $pwd = md5($password . $admin['salt']);
        if ($pwd != $admin['password']){
            ShowMsg("密码错误");
            exit;
        }
        $session_imgcode = strtolower($_SESSION['imgcode']);
        $imgcode_ = strtolower($imgcode);
        if ($imgcode_ != $session_imgcode) {
            ShowMsg("验证码错误");
            exit;
        }
        setcookie('username', md5($admin['id'] . $admin['salt']));
        ShowMsg("登录成功", "index.php");
        exit;
    }
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>login</title>
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <link rel="stylesheet" type="text/css" href="static/lib/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="static/stylesheets/theme.css">
    <link rel="stylesheet" href="static/lib/font-awesome/css/font-awesome.css">

</head>

<body>
    <!--<![endif]-->

    <div class="navbar">
        <div class="navbar-inner">
            <a class="brand" href="index.html"><span class="second">Admin</span></a>
        </div>
    </div>

    <div class="row-fluid">
        <div class="dialog">
            <div class="block">
                <p class="block-heading">登录</p>
                <div class="block-body">
                    <form method="POST">
                        <label>用户名</label>
                        <input type="text" class="span12" name="username">
                        <label>密码</label>
                        <input type="password" class="span12" name="password">
                        <label for="">验证码</label>
                        <input type="text" class="span6" name="imgcode">
                        <img src="./config/imgcode.php" alt="" onclick="this.src = `./config/imgcode.php`">
                        <button class="btn btn-primary pull-right" type="submit">登录</button>
                        <div class="clearfix"></div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <!-- <script type="text/javascript">
        $("[rel=tooltip]").tooltip();
        $(function() {
            $('.demo-cancel-click').click(function() {
                return false;
            });
        });
    </script> -->
</body>

</html>
<script src="static/lib/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="static/lib/bootstrap/js/bootstrap.js"></script>